<?php

/**
 * NOTICE OF LICENSE
 *
 * THIS SOURCE FILE IS PART OF EVEBIT'S PRIVATE PROJECT.
 * 
 * DO NOT USE THIS FILE IN OTHER PLACE.
 *
 * @category    EveBit_Library
 * @package     Application
 * @author      Cheng Wei <berlios.o@gmail.com>
 * @version     $$Id: View.php 130 2011-03-18 03:10:02Z cheng.wei $$
 * @copyright   Copyright (c) 2008 Evebit Inc. China (http://www.evebit.com)
 */

/**
 * EveBit View
 * 
 * 1, render a block template
 * There have many public templates in application/modules/blocks/public-views/scripts/ 
 * You could use them any time in a view.
 * Just call like this:
 * <code>
 * $html = $this->renderBlock("register-email", 
 * array('customer_email'=>'Jim'));
 * </code>
 * 
 * It will find template file 'register-email.phtml' under that folder.
 * Then assign variable 'Jim' as 'customer_email'.
 * Then render and return output.
 *
 * 2, translate in view
 * See Evebit_Application, it translate as that one.
 * 
 * <code>
 * 
 * $this->__('original string');
 * 
 * $this->__('format string', '0123');
 * 
 * $this->__(array('format string', '0123'));
 * 
 * </code>
 * 
 * 
 * @docinfo
 * 
 * @uses      Zend_View
 * 
 * @category    EveBit_Library
 * @package     Application
 * @author      Cheng Wei <berlios.o@gmail.com>
 * @version     $$Id: View.php 130 2011-03-18 03:10:02Z cheng.wei $$
 */
class Evebit_View extends Zend_View {

	/**
	 * @var Evebit_Application_Session
	 */
	protected $_session;

	/**
	 * @var Evebit_Application_View
	 */
	protected $blockView = null;

	/**
	 * Translate into current locale
	 * 
	 * e.g. 
	 * 
	 * there are to string in language file:
	 * <code>
	 * original string=show this text
	 * format string=first arg is %s
	 * </code>
	 * 
	 * then, you can tranlsate as:
	 * <code>
	 * 
	 * Evebit_Application_Session::__('original string');
	 * // result is 'show this text'
	 * 
	 * Evebit_Application_Session::__('format string', '0123');
	 * // result is 'first arg is 0123'
	 * 
	 * Evebit_Application_Session::__(array('format string', '0123'));
	 * // result is 'first arg is 0123'
	 * 
	 * </code>
	 * 
	 * @XXX: the check for missing translation is wrong
	 * 
	 * @see Evebit_Application_Session::translate
	 * 
	 * @param string|array $msg
	 * @return string
	 */
	public function __($msg) {
		$args = func_get_args();
		$aftmsg = $this->getSession()->translate($args);
		if ($msg === $aftmsg)
			Evebit_Event::event('insert_translater_data', $aftmsg, 
				Zend_Registry::get('Zend_Translate'), $this->getSession()->getLocale());
		return $aftmsg;
	}

	/**
	 * Get current session instance
	 * 
	 * @see Evebit_Application
	 * @see Evebit_Application_Session
	 * 
	 * @return Evebit_Application_Session
	 */
	public function getSession() {
		if (!$this->_session) {
			$this->_session = Evebit_Application::getSession();
		}
		return $this->_session;
	}

	/**
	 * Get a system configuration
	 * 
	 * @see Evebit_Application::config
	 * @return string|array
	 */
	public function getConfig($path) {
		return Evebit_Application::config($path);
	}

	/**
	 * Render a block template
	 * 
	 * 
	 * 
	 * @param string $script
	 * @param array $params
	 * 
	 * @return string
	 */
	public function renderBlock($script, $params = array()) {
		if (!$this->blockView) {
			$view = clone $this;
			$view->addScriptPath(APPLICATION_PATH . '/modules/blocks/public-views/scripts/');
			$this->blockView = $view;
		}
		
		$view = $this->blockView;
		if (is_array($params)) {
			foreach ($params as $key => $val) {
				$view->assign($key, $val);
			}
		}
		
		$html = $view->render($script . '.phtml');
		return $html;
	}
}
